package org.emet.dao;

import org.emet.entity.biz.ServiceItem;
import org.emet.jee.core.dao.Page;
import org.emet.jee.core.dao.arch.ArchDataSourceBaseDao;
import org.emet.service.dto.ServiceItemCriteria;
import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

@Repository
public class ServiceItemDao extends ArchDataSourceBaseDao<ServiceItem, Long> {
	public Page<ServiceItem> search(Page<ServiceItem> page, ServiceItemCriteria criteria) {
		Criteria c = getSession().createCriteria(ServiceItem.class);
		NonEmptyValueHashMap<String, Object> propertyNameValues = new NonEmptyValueHashMap<String, Object>();
		propertyNameValues.put("code", criteria.getCode());
		propertyNameValues.put("name", criteria.getName());
		propertyNameValues.put("unit", criteria.getUnit());
		propertyNameValues.put("singlePrice", criteria.getSinglePrice(), 0d);
		propertyNameValues.put("remark", criteria.getRemark());
		c.add(Restrictions.allEq(propertyNameValues));
		page.setOrderBy("id");
		page.setOrder("desc");
		return findPage(page, c);
	}

	public boolean isExist(Long id, String code, String name) {
		long count = (Long) findUnique("select count(*) from ServiceItem where id <> ? and (code = ? or name=?)", id,
				code, name);
		return count > 0 ? true : false;
	}
}
